#include<bits/stdc++.h>
#define MAX_N 1001
using namespace std;
//1.先找环：类似于拓扑排序，把度为1的点删掉 O(N)
//2.计算距离：BFS/DFS，从环路中的点出发，遍历图计算距离 O(N)
int main()
{
    int T;
    cin >> T;
    for (int ti = 1; ti <= T; ++ti)
    {
        int N;
        cin >> N;
        // Construct the graph （邻接表）
        // vector<int> G[MAX_N];
        vector<vector<int>> G(MAX_N);
        for (int i = 0; i < N; ++i)
        {
            int x, y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        // Compute the degree for each node
        queue<int> q;
        vector<int> degree(N + 1);
        for (int i = 1; i <= N; ++i)
        {
            degree[i] = G[i].size();
            if (G[i].size() == 1) q.push(i);
        }
        // Topological sort 
        vector<int> dis(N + 1);
        while (!q.empty())
        {
            int node = q.front();
            q.pop();
            dis[node] = -1;
            for (int i = 0; i < G[node].size(); ++i)
            {
                int v = G[node][i];
                degree[v]--;
                if (degree[v] == 1) q.push(v);
            }
        }
        // Add in nodes in the cycle 最终dis值为0的点是在环路中的点
        for (int i = 1; i <= N; ++i)
        {
            if (dis[i] == 0) q.push(i);
        }
        // BFS to compute the distance 
        while (!q.empty())
        {
            int node = q.front();
            q.pop();
            for (int i = 0; i < G[node].size(); ++i)
            {
                int v = G[node][i];
                if (dis[v] == -1) 
                {
                    dis[v] = dis[node] + 1; //BFS肯定是由近到远，不用对比距离
                    q.push(v);
                }
            }
        }

        cout << "Case #" << ti << ": ";
        for (int i = 1; i <= N; ++i)
        {
            cout << dis[i] << " ";
        }
        cout << endl;
    }
    return 0;
}